<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>menuitem.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_menuitem.js.html">menuitem.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A class for representing items in menus.
<a name="line17"></a> * @see goog.ui.Menu
<a name="line18"></a> *
<a name="line19"></a> * @see ../demos/menuitem.html
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.ui.MenuItem&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.array&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line28"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line29"></a>goog.require(&#39;goog.string&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.Control&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.ControlContent&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.MenuItemRenderer&#39;);
<a name="line34"></a>goog.require(&#39;goog.ui.registry&#39;);
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Class representing an item in a menu.
<a name="line40"></a> *
<a name="line41"></a> * @param {goog.ui.ControlContent} content Text caption or DOM structure to
<a name="line42"></a> *     display as the content of the item (use to add icons or styling to
<a name="line43"></a> *     menus).
<a name="line44"></a> * @param {*=} opt_model Data/model associated with the menu item.
<a name="line45"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper used for
<a name="line46"></a> *     document interactions.
<a name="line47"></a> * @param {goog.ui.MenuItemRenderer=} opt_renderer Optional renderer.
<a name="line48"></a> * @constructor
<a name="line49"></a> * @extends {goog.ui.Control}
<a name="line50"></a> */
<a name="line51"></a>goog.ui.MenuItem = function(content, opt_model, opt_domHelper, opt_renderer) {
<a name="line52"></a>  goog.ui.Control.call(this, content, opt_renderer ||
<a name="line53"></a>      goog.ui.MenuItemRenderer.getInstance(), opt_domHelper);
<a name="line54"></a>  this.setValue(opt_model);
<a name="line55"></a>};
<a name="line56"></a>goog.inherits(goog.ui.MenuItem, goog.ui.Control);
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * The access key for this menu item. This key allows the user to quickly
<a name="line61"></a> * trigger this item&#39;s action with they keyboard. For example, setting the
<a name="line62"></a> * mnenomic key to 70 (F), when the user opens the menu and hits &quot;F,&quot; the
<a name="line63"></a> * menu item is triggered.
<a name="line64"></a> *
<a name="line65"></a> * @type {goog.events.KeyCodes}
<a name="line66"></a> * @private
<a name="line67"></a> */
<a name="line68"></a>goog.ui.MenuItem.mnemonicKey_;
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * The class set on an element that contains a parenthetical mnemonic key hint.
<a name="line73"></a> * Parenthetical hints are added to items in which the mnemonic key is not found
<a name="line74"></a> * within the menu item&#39;s caption itself. For example, if you have a menu item
<a name="line75"></a> * with the caption &quot;Record,&quot; but its mnemonic key is &quot;I&quot;, the caption displayed
<a name="line76"></a> * in the menu will appear as &quot;Record (I)&quot;.
<a name="line77"></a> *
<a name="line78"></a> * @type {string}
<a name="line79"></a> * @private
<a name="line80"></a> */
<a name="line81"></a>goog.ui.MenuItem.MNEMONIC_WRAPPER_CLASS_ =
<a name="line82"></a>    goog.getCssName(&#39;goog-menuitem-mnemonic-separator&#39;);
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * The class set on an element that contains a keyboard accelerator hint.
<a name="line87"></a> * @type {string}
<a name="line88"></a> * @private
<a name="line89"></a> */
<a name="line90"></a>goog.ui.MenuItem.ACCELERATOR_CLASS_ = goog.getCssName(&#39;goog-menuitem-accel&#39;);
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>// goog.ui.Component and goog.ui.Control implementation.
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Returns the value associated with the menu item.  The default implementation
<a name="line98"></a> * returns the model object associated with the item (if any), or its caption.
<a name="line99"></a> * @return {*} Value associated with the menu item, if any, or its caption.
<a name="line100"></a> */
<a name="line101"></a>goog.ui.MenuItem.prototype.getValue = function() {
<a name="line102"></a>  var model = this.getModel();
<a name="line103"></a>  return model != null ? model : this.getCaption();
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * Sets the value associated with the menu item.  The default implementation
<a name="line109"></a> * stores the value as the model of the menu item.
<a name="line110"></a> * @param {*} value Value to be associated with the menu item.
<a name="line111"></a> */
<a name="line112"></a>goog.ui.MenuItem.prototype.setValue = function(value) {
<a name="line113"></a>  this.setModel(value);
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Sets the menu item to be selectable or not.  Set to true for menu items
<a name="line119"></a> * that represent selectable options.
<a name="line120"></a> * @param {boolean} selectable Whether the menu item is selectable.
<a name="line121"></a> */
<a name="line122"></a>goog.ui.MenuItem.prototype.setSelectable = function(selectable) {
<a name="line123"></a>  this.setSupportedState(goog.ui.Component.State.SELECTED, selectable);
<a name="line124"></a>  if (this.isChecked() &amp;&amp; !selectable) {
<a name="line125"></a>    this.setChecked(false);
<a name="line126"></a>  }
<a name="line127"></a>
<a name="line128"></a>  var element = this.getElement();
<a name="line129"></a>  if (element) {
<a name="line130"></a>    this.getRenderer().setSelectable(this, element, selectable);
<a name="line131"></a>  }
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Sets the menu item to be checkable or not.  Set to true for menu items
<a name="line137"></a> * that represent checkable options.
<a name="line138"></a> * @param {boolean} checkable Whether the menu item is checkable.
<a name="line139"></a> */
<a name="line140"></a>goog.ui.MenuItem.prototype.setCheckable = function(checkable) {
<a name="line141"></a>  this.setSupportedState(goog.ui.Component.State.CHECKED, checkable);
<a name="line142"></a>
<a name="line143"></a>  var element = this.getElement();
<a name="line144"></a>  if (element) {
<a name="line145"></a>    this.getRenderer().setCheckable(this, element, checkable);
<a name="line146"></a>  }
<a name="line147"></a>};
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * Returns the text caption of the component while ignoring accelerators.
<a name="line152"></a> * @override
<a name="line153"></a> */
<a name="line154"></a>goog.ui.MenuItem.prototype.getCaption = function() {
<a name="line155"></a>  var content = this.getContent();
<a name="line156"></a>  if (goog.isArray(content)) {
<a name="line157"></a>    var acceleratorClass = goog.ui.MenuItem.ACCELERATOR_CLASS_;
<a name="line158"></a>    var mnemonicWrapClass = goog.ui.MenuItem.MNEMONIC_WRAPPER_CLASS_;
<a name="line159"></a>    var caption = goog.array.map(content, function(node) {
<a name="line160"></a>      var classes = goog.dom.classes.get(node);
<a name="line161"></a>      if (goog.array.contains(classes, acceleratorClass) ||
<a name="line162"></a>          goog.array.contains(classes, mnemonicWrapClass)) {
<a name="line163"></a>        return &#39;&#39;;
<a name="line164"></a>      } else {
<a name="line165"></a>        return goog.dom.getRawTextContent(node);
<a name="line166"></a>      }
<a name="line167"></a>    }).join(&#39;&#39;);
<a name="line168"></a>    return goog.string.collapseBreakingSpaces(caption);
<a name="line169"></a>  }
<a name="line170"></a>  return goog.ui.MenuItem.superClass_.getCaption.call(this);
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/** @override */
<a name="line175"></a>goog.ui.MenuItem.prototype.handleMouseUp = function(e) {
<a name="line176"></a>  var parentMenu = /** @type {goog.ui.Menu} */ (this.getParent());
<a name="line177"></a>
<a name="line178"></a>  if (parentMenu) {
<a name="line179"></a>    var oldCoords = parentMenu.openingCoords;
<a name="line180"></a>    // Clear out the saved opening coords immediately so they&#39;re not used twice.
<a name="line181"></a>    parentMenu.openingCoords = null;
<a name="line182"></a>
<a name="line183"></a>    if (oldCoords &amp;&amp; goog.isNumber(e.clientX)) {
<a name="line184"></a>      var newCoords = new goog.math.Coordinate(e.clientX, e.clientY);
<a name="line185"></a>      if (goog.math.Coordinate.equals(oldCoords, newCoords)) {
<a name="line186"></a>        // This menu was opened by a mousedown and we&#39;re handling the consequent
<a name="line187"></a>        // mouseup. The coords haven&#39;t changed, meaning this was a simple click,
<a name="line188"></a>        // not a click and drag. Don&#39;t do the usual behavior because the menu
<a name="line189"></a>        // just popped up under the mouse and the user didn&#39;t mean to activate
<a name="line190"></a>        // this item.
<a name="line191"></a>        return;
<a name="line192"></a>      }
<a name="line193"></a>    }
<a name="line194"></a>  }
<a name="line195"></a>
<a name="line196"></a>  goog.base(this, &#39;handleMouseUp&#39;, e);
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/** @override */
<a name="line201"></a>goog.ui.MenuItem.prototype.handleKeyEventInternal = function(e) {
<a name="line202"></a>  if (e.keyCode == this.getMnemonic() &amp;&amp; this.performActionInternal(e)) {
<a name="line203"></a>    return true;
<a name="line204"></a>  } else {
<a name="line205"></a>    return goog.base(this, &#39;handleKeyEventInternal&#39;, e);
<a name="line206"></a>  }
<a name="line207"></a>};
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Sets the mnemonic key code. The mnemonic is the key associated with this
<a name="line212"></a> * action.
<a name="line213"></a> * @param {goog.events.KeyCodes} key The key code.
<a name="line214"></a> */
<a name="line215"></a>goog.ui.MenuItem.prototype.setMnemonic = function(key) {
<a name="line216"></a>  this.mnemonicKey_ = key;
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/**
<a name="line221"></a> * Gets the mnemonic key code. The mnemonic is the key associated with this
<a name="line222"></a> * action.
<a name="line223"></a> * @return {goog.events.KeyCodes} The key code of the mnemonic key.
<a name="line224"></a> */
<a name="line225"></a>goog.ui.MenuItem.prototype.getMnemonic = function() {
<a name="line226"></a>  return this.mnemonicKey_;
<a name="line227"></a>};
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>// Register a decorator factory function for goog.ui.MenuItems.
<a name="line231"></a>goog.ui.registry.setDecoratorByClassName(goog.ui.MenuItemRenderer.CSS_CLASS,
<a name="line232"></a>    function() {
<a name="line233"></a>      // MenuItem defaults to using MenuItemRenderer.
<a name="line234"></a>      return new goog.ui.MenuItem(null);
<a name="line235"></a>    });
</pre>


</body>
</html>
